package org.onvivals.admin.module.pms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.pms.entity.PmsRoleIndividuality;
import org.onvivals.module.pms.param.PmsRoleIndividualityParam;
import org.onvivals.module.pms.service.IPmsRoleIndividualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "角色个性管理")
@RestController
@RequestMapping("/pms/role/individuality")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PmsRoleIndividualityController {

	private final IPmsRoleIndividualityService pmsRoleIndividualityService;

	@Operation(summary = "分页查询角色个性")
	@GetMapping("/page")
	public Result<PageResult<PmsRoleIndividuality>> page(PmsRoleIndividualityParam param) {
		PageParam<PmsRoleIndividuality, PmsRoleIndividualityParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
		IPage<PmsRoleIndividuality> pa = pmsRoleIndividualityService.page(page, page.getWrapper());
		PageResult<PmsRoleIndividuality> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
		return Result.OK("查询成功", pageResult);
	}


	@Operation(summary = "根据id查询角色")
	@GetMapping("/{id}")
	public Result<PmsRoleIndividuality> get(@PathVariable("id") Long id) {
		PmsRoleIndividuality PmsRole = pmsRoleIndividualityService.getById(id);
		return Result.OK("查询成功", PmsRole);
	}

	@Operation(summary = "添加角色")
	@PostMapping()
	public Result<?> save(@RequestBody PmsRoleIndividuality PmsRole) {
		if (pmsRoleIndividualityService.save(PmsRole)) {
			return Result.OK("添加成功");
		}
		return Result.error("添加失败");
	}

	@Operation(summary = "修改角色")
	@PutMapping()
	public Result<?> update(@RequestBody PmsRoleIndividuality PmsRole) {
		if (pmsRoleIndividualityService.updateById(PmsRole)) {
			return Result.OK("修改成功");
		}
		return Result.error("修改失败");
	}

	@Operation(summary = "删除角色")
	@DeleteMapping("/{id}")
	public Result<?> remove(@PathVariable("id") Long id) {
		if (pmsRoleIndividualityService.removeById(id)) {
			return Result.OK("删除成功");
		}
		return Result.error("删除失败");
	}

}
